package com.unlcn.erp.hyc.interfaces.api.controller;

import cn.huiyunche.commons.domain.ResultDTO;
import cn.huiyunche.commons.exception.BusinessException;
import com.unlcn.erp.hyc.interfaces.backend.service.BackHYCOrderService;
import com.unlcn.erp.hyc.interfaces.backend.service.CallWayBillByHYCService;
import com.unlcn.erp.hyc.interfaces.backend.service.ExecptionDisposeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by zhiche024 on 2017/9/6.
 */
@Controller
@RequestMapping("/callWayBillByHYC")
public class CallWayBillByHYCController {

    private static final Logger LOGGER = LoggerFactory.getLogger(CallWayBillByHYCController.class);

    @Autowired
    private CallWayBillByHYCService callWayBillByHYCService;

    @Autowired
    private BackHYCOrderService backHYCOrderService;

    @Autowired
    private ExecptionDisposeService execptionDisposeService;

    /**
     * 存入慧运车订单
     */
    @RequestMapping(value = "/saveWayBillByHYC", method = RequestMethod.POST)
    public  ResultDTO<Object> saveWayBillByHYC(){
        LOGGER.info("CallOrderByHYCController.saveWayBillByHYC param:{}.");
        ResultDTO<Object> result = new ResultDTO<Object>(true, null, "运单保存成功");
        try{
            callWayBillByHYCService.getWayBillCodes();
        }catch (Exception e){
            LOGGER.error("TmsPickUpController.getExcepList error: {}", e);
            result.setSuccess(false);
            result.setMessage("运单保存失败");
            throw new BusinessException(e.getMessage());
        }

        return  result;
    }

    @RequestMapping(value = "/backHYCByWayBillArrived", method = RequestMethod.POST)
    public ResultDTO<Object> backHYCByWayBillArrived(){
        LOGGER.info("CallOrderByHYCController.saveWayBillByHYC param:{}.");
        ResultDTO<Object> result = new ResultDTO<Object>(true, null, "运抵操作成功");
        try{
            backHYCOrderService.backHYCByWayBillArrived();
        }catch (Exception e){
            LOGGER.error("TmsPickUpController.getExcepList error: {}", e);
            result.setSuccess(false);
            result.setMessage("运抵操作失败");
            throw new BusinessException(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/doDisposeExe", method = RequestMethod.POST)
    public ResultDTO<Object> doDisposeExe(){
        LOGGER.info("CallOrderByHYCController.saveWayBillByHYC param:{}.");
        ResultDTO<Object> result = new ResultDTO<Object>(true, null, "运单保存成功");
        try{
            execptionDisposeService.doDisposeExe();
        }catch (Exception e){
            LOGGER.error("TmsPickUpController.getExcepList error: {}", e);
            result.setSuccess(false);
            result.setMessage("运单保存失败");
            throw new BusinessException(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/doSaveWaybill",method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO<Object> doSaveWaybill(String waybill){
        LOGGER.info("CallOrderByHYCController.doSaveWaybill param:{}.");
        ResultDTO<Object> result = new ResultDTO<Object>(true, null, "运单保存成功");
        try{
            callWayBillByHYCService.saveWayBillDetails(waybill);
        }catch (Exception e){
            LOGGER.error("TmsPickUpController.getExcepList error: {}", e);
            result.setSuccess(false);
            result.setMessage("运单保存失败");
            throw new BusinessException(e.getMessage());
        }
        return result;
    }

    /**
     *  根据错误执行运单
     * @param waybill
     * @return
     */
    @RequestMapping(value = "/doDisposeExeByCode", method = RequestMethod.POST)
    @ResponseBody
    public ResultDTO<Object>  doDisposeExeByCode(String waybill){
        LOGGER.info("CallOrderByHYCController.saveWayBillByHYC param:{}.");
        ResultDTO<Object> result = new ResultDTO<Object>(true, null, "运单保存成功");
        try{
            execptionDisposeService.doDisposeExeByCode(waybill);
        }catch (Exception e){
            LOGGER.error("TmsPickUpController.getExcepList error: {}", e);
            result.setSuccess(false);
            result.setMessage("运单保存失败");
            throw new BusinessException(e.getMessage());
        }

        return result;
    }

    /**
     * 批量导入hyc运单
     */
    @RequestMapping(value = "/doSaveWaybillList", method = RequestMethod.POST)
    public ResultDTO<Object> doSaveWaybillList(){
        LOGGER.info("CallOrderByHYCController.doSaveWaybillList param:{}.");
        ResultDTO<Object> result = new ResultDTO<Object>(true, null, "运单保存成功");
        try{
            callWayBillByHYCService.doSaveWaybillList();
        }catch (Exception e){
            LOGGER.error("TmsPickUpController.doSaveWaybillList error: {}", e);
            result.setSuccess(false);
            result.setMessage("运单保存失败");
            throw new BusinessException(e.getMessage());
        }
        return result;
    }


    /**
     * 修改运单金额
     */
    @RequestMapping(value = "/dateAmend", method = RequestMethod.POST)
    public ResultDTO<Object>  dateAmend(){
        LOGGER.info("CallOrderByHYCController.doSaveWaybillList param:{}.");
        ResultDTO<Object> result = new ResultDTO<Object>(true, null, "运单保存成功");
        try{
            execptionDisposeService.dateAmend();
        }catch (Exception e){
            LOGGER.error("TmsPickUpController.getExcepList error: {}", e);
            result.setSuccess(false);
            result.setMessage("运单保存失败");
            throw new BusinessException(e.getMessage());
        }
        return result;
    }
}
